﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Controllers;

namespace Web
{
    public partial class InvestorEdit : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                ApolloOaDataContext ctx = new ApolloOaDataContext(ConfigurationManager.ConnectionStrings["SqlConnectionString"].ConnectionString);
                var listIM = ctx.CMS_Intermediations.Where(r=>r.IsValid).OrderBy(r => r.Name);
                IMID.DataSource = listIM;
                IMID.DataTextField = "Name";
                IMID.DataValueField = "Id";
                IMID.DataBind();
                IMID.Items.Insert(0, new ListItem("", ""));

                var listMarket = ctx.Employees.OrderBy(r => r.Name);
                MarketID.DataSource = listMarket;
                MarketID.DataTextField = "Name";
                MarketID.DataValueField = "Id";
                MarketID.DataBind();
                MarketID.Items.Insert(0, new ListItem("", ""));

                if (Request.Params["Id"] != null)
                {
                    int id = int.Parse(Request.Params["Id"]);

                    CMS_Investor model = ctx.CMS_Investor.Single(q => q.Id == id);
                    InvestorNO.Value = model.InvestorNO;
                    Name.Value = model.Name;
                    Sex.Value = model.Sex;
                    IDNO.Value = model.IDNO;
                    Tel.Value = model.Tel;
                    EMail.Value = model.EMail;
                    Address.Value = model.Address;
                    EduLevel.Value = model.EduLevel;
                    MaritalStatus.Value = model.MaritalStatus;
                    Occupation.Value = model.Occupation;
                    Income.Value = model.Income;
                    Character.Value = model.Character;
                    IMID.Value = model.IMID.ToString();
                    MarketID.Value = model.MarketID.ToString();
                    StarLevel.Value = model.StarLevel;
                    CommissionRate.Value = model.CommissionRate.ToString();
                    InvestMSG.Value = model.InvestMSG;
                    RiskLevel.Value = model.RiskLevel;
                    BankName.Value = model.BankName;
                    BankAccount.Value = model.BankAccount;
                    Brokerage.Value = model.Brokerage;
                    Remarks.Value = model.Remarks;
                }
            }
        }
        protected void btnSave_Click(object sender, EventArgs e)
        {
            ApolloOaDataContext ctx = new ApolloOaDataContext(ConfigurationManager.ConnectionStrings["SqlConnectionString"].ConnectionString);
            CMS_Investor model;
            string id = Request.QueryString["id"];
            if (!string.IsNullOrEmpty(id))
            {
                model = ctx.CMS_Investor.Single(r => r.Id == int.Parse(id));
            }
            else
            {
                model = new CMS_Investor();
                ctx.CMS_Investor.InsertOnSubmit(model);
            }
            model.InvestorNO = InvestorNO.Value;
            model.Name = Name.Value;
            model.Sex = Sex.Value;
            model.IDNO = IDNO.Value;
            model.Tel = Tel.Value;
            model.EMail = EMail.Value;
            model.Address = Address.Value;
            model.EduLevel = EduLevel.Value;
            model.MaritalStatus = MaritalStatus.Value;
            model.Occupation = Occupation.Value;
            model.Income = Income.Value;
            model.Character = Character.Value;
            model.IMID = int.Parse(IMID.Value);
            model.MarketID = int.Parse(MarketID.Value);
            model.StarLevel = StarLevel.Value;
            model.CommissionRate = decimal.Parse(CommissionRate.Value);
            model.InvestMSG = InvestMSG.Value;
            model.RiskLevel = RiskLevel.Value;
            model.BankName = BankName.Value;
            model.BankAccount = BankAccount.Value;
            model.Brokerage = Brokerage.Value;
            model.Remarks = Remarks.Value;
            ctx.SubmitChanges();
            Response.Write("<script>window.returnValue='Succeed';window.close();</script>");
        }
    }
}